import { ref, reactive, onMounted, nextTick } from 'vue';
import { emitter } from "@/utils/mitt";
import { ElMessage, FormInstance } from "element-plus";
import {
  ProOBDDataObtaAuth,
  ProProtectionGetOBDData,
  ProProtectionGetPINumber,
  ProProtectionGetApplysHealth,
  ProProtectionEmpowerHealthData
} from "@/api/stu/InsuranceData";
import { type PaginationProps } from "@pureadmin/table";
import qs from 'qs';
import { useRoute } from "vue-router";
export function useTDataManagement() {
  const route = useRoute();
  const columns = [
    {
      label: "序号",
      type: "index",
      minWidth: 100
    }, {
      label: "保单号",
      prop: "piNumber",
    },
    {
      label: "投保人",
      prop: "piApplicant",
    },
    {
      label: "保险产品名称",
      prop: "ppName",
    },
    {
      label: "保险类型",
      prop: "pType",
    }, {
      label: "操作",
      fixed: "right",
      width: 220,
      slot: "operation"
    }

  ];
  const dataLists = ref([]);
  const loadings = ref(true);
  const dataList = ref([]);
  const loading = ref(true);
  const DialogVisible = ref(false);
  const statement = ref(false);
  const btnloading = ref<boolean>(false);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 30,
    currentPage: 1,
    pageSizes: [10, 30, 50, 100],
    background: true
  });
  const paginations = reactive<PaginationProps>({
    total: 0,
    pageSize: 30,
    currentPage: 1,
    pageSizes: [10, 30, 50, 100],
    background: true
  });
  const stubimfrom = ref()
  const PINumber = ref([]);
  const SendDataName = ref<any>({})// 授权名称
  const SendDataDate = ref(undefined);// 授权时间
  function handleSizeChange() { GetListData() }
  function handleCurrentChange() { GetListData() };
  function handleSizeChanges() { GetListDatas() }
  function handleCurrentChanges() { GetListDatas() };
  // 关闭申请窗口
  function close_from(row) {
    DialogVisible.value = false;
    row.resetFields();
  }

  async function SendData(row) {
    // if (row.getCar === 2) {
    //   const res = await ProOBDDataObtainEmp(qs.stringify({ PINumber: row.piNumber }))
    //   if (res.success) {
    //     ElMessage.success(res.msg)
    //     GetListData()
    //   } else {
    //     ElMessage.error(res.msg)
    //   }
    //   return
    // }
    statement.value = true;
    nextTick(() => {
      SendDataName.value = row
    })
  }

  async function SendDatas(row) {
    const data = {
      "taskId": route.query.taskId,
      "planId": route.query.planId,
      piNumber: row.piNumber,
      ppName: row.ppName,
      piApplicant: row.piApplicant
    }
    const res = await ProProtectionEmpowerHealthData(data)
    if (res.success) {
      ElMessage.success(res.msg)
      GetListDatas()
    } else {
      ElMessage.error(res.msg)
    }
  }

  // 发送授权声明
  async function SendAccredit() {
    if (!SendDataDate.value) {
      ElMessage.warning("请选择授权时间");
      return
    }
    const data = {
      "taskId": route.query.taskId,
      "planId": route.query.planId,
      piNumber: SendDataName.value.piNumber,
      empowerTime: SendDataDate.value,
      ppName: SendDataName.value.ppName,
      name: SendDataName.value.piApplicant
    };
    btnloading.value = true;
    const res = await ProOBDDataObtaAuth(data);
    if (res.success) {
      ElMessage.success(res.msg)
      CloseAccredit()
      GetListData()
      btnloading.value = false;
    } else {
      ElMessage.error(res.msg)
      setTimeout(() => { btnloading.value = false; }, 1500)
    }
  }
  // 关闭取消授权声明
  function CloseAccredit() {
    statement.value = false;
    SendDataDate.value = undefined;
    SendDataName.value = {}
  }

  // 获取数据
  async function GetBD() {
    const data = qs.stringify({
      "taskId": route.query.taskId,
      "planId": route.query.planId,
    });
    const res = await ProProtectionGetPINumber(data);
    if (res.success) {
      PINumber.value = res.data
    } else {
      ElMessage.error(res.msg);
    }
  }
  // 获取OBD数据
  async function GetListData() {
    const data = {
      "pageIndex": pagination.currentPage,
      "pageCount": pagination.pageSize,
      "taskId": route.query.taskId,
      "planId": route.query.planId,
    };
    const res = await ProProtectionGetOBDData(data);
    if (res.success) {
      dataList.value = res.data?.items;
      pagination.total = res.data?.totalCount;
      loading.value = false;
    } else {
      ElMessage.error(res.msg);
      setTimeout(() => { loading.value = false; }, 1500)
    }
  }

  // 获取健康数据
  async function GetListDatas() {
    const data = {
      "pageIndex": paginations.currentPage,
      "pageCount": paginations.pageSize,
      "taskId": route.query.taskId,
      "planId": route.query.planId,
    };
    const res = await ProProtectionGetApplysHealth(data);
    if (res.success) {
      dataLists.value = res.data?.items;
      paginations.total = res.data?.totalCount;
      loadings.value = false;
    } else {
      ElMessage.error(res.msg);
      setTimeout(() => { loadings.value = false; }, 1500)
    }
  }
  onMounted(() => {
    GetBD()
    GetListData()
    GetListDatas()
  })
  return {
    columns,
    dataList,
    loading,
    PINumber,
    statement,
    stubimfrom,
    DialogVisible,
    SendDataName,
    pagination,
    SendDataDate,
    dataLists,
    paginations,
    btnloading,
    loadings,
    SendData,
    SendDatas,
    close_from,
    SendAccredit,
    CloseAccredit,
    handleSizeChange,
    handleSizeChanges,
    handleCurrentChanges,
    handleCurrentChange
  }
}
